/**
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.profile;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import com.loopj.android.http.ResponseHandlerInterface;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.camera.CameraActivity;
import com.tstudy.laoshibang.custom.BirthdaySelectView;
import com.tstudy.laoshibang.custom.BorderTextView;
import com.tstudy.laoshibang.event.SelectAddressEvent;
import com.tstudy.laoshibang.event.UpdateInfoEvent;
import com.tstudy.laoshibang.event.UploadAvatarEvent;
import com.tstudy.laoshibang.login.BindMobileFragment;
import com.tstudy.laoshibang.login.NeedLoginFragment;
import com.tstudy.laoshibang.manager.DictionaryManager;
import com.tstudy.laoshibang.manager.UserManager;
import com.tstudy.laoshibang.mode.DicMap;
import com.tstudy.laoshibang.mode.Dictionary;
import com.tstudy.laoshibang.mode.response.BaseResponse;
import com.tstudy.laoshibang.mode.response.FileUploadResponse;
import com.tstudy.laoshibang.mode.response.UserResponse;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import de.greenrobot.event.EventBus;
@EFragment(R.layout.user_info)
public class UserInfoFragment extends NeedLoginFragment{
	final static String TAG = "user_info";
	public UserInfoFragment() {
		super();
	}
	
	@ViewById(R.id.user_info_root)
	RelativeLayout mRootLayout;
	
	@ViewById(R.id.user_info_nick_value)
	TextView mNickValue;
	
	@ViewById(R.id.user_info_level_value)
	TextView mLevelValue;
	
	@ViewById(R.id.user_info_school_value)
	TextView mSchoolValue;
	
	@ViewById(R.id.user_info_account_value)
	TextView mAccountValue;
	
	@ViewById(R.id.user_info_district_value)
	TextView mDistrictValue;
	
	@ViewById(R.id.user_info_name_value)
	TextView mNameValue;
	
	@ViewById(R.id.user_info_email_value)
	TextView mEmailValue;
	
	@ViewById(R.id.user_info_birthday_value)
	TextView mBirthdayValue;
	
	@ViewById(R.id.user_info_sex_value)
	TextView mSexValue;
	
	@ViewById(R.id.user_info_avatar)
	ImageView mAvatarImg;
	
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;

	public static void add(int containerViewId) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,UserInfoFragment_.class.getName(),args);
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId, fragment, TAG);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		EventBus.getDefault().register(this);
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		if (mIsFirstLoad) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					getUserInfo();
				}
			},CONSTANT.LOAD_DATA_DELAY_TIME);
		}
	}
	public void onEventMainThread(Object object) {
		if (object instanceof SelectAddressEvent ) {
			SelectAddressEvent addressEvents = (SelectAddressEvent)object;
			if (addressEvents != null) {
				if (addressEvents.selectType.equals(Dictionary.TYPE_CITY)) {//选择地区
					mDistrictValue.setText(addressEvents.firstCol.name + " " + addressEvents.secondCol.name);
					DicMap province = new DicMap(addressEvents.firstCol.code,addressEvents.firstCol.name);
					DicMap city = new DicMap(addressEvents.secondCol.code,addressEvents.secondCol.name);
					DicMap county = new DicMap(-1,"");
					updateSchool(province, city, county, "-1", "");
				}else if(addressEvents.selectType.equals(Dictionary.TYPE_SCHOOL)){//选择学校
					mDistrictValue.setText(mDistrictValue.getText() + addressEvents.firstCol.name);
					mSchoolValue.setText(addressEvents.secondCol.name);
					BaseApplication.mCurrentUser.county = new DicMap(addressEvents.firstCol.code,addressEvents.firstCol.name);
					BaseApplication.mCurrentUser.school = new DicMap(addressEvents.secondCol.code,addressEvents.secondCol.name);
					int schoolCode = addressEvents.secondCol.code;
					String schoolName = addressEvents.secondCol.name;
					updateSchool(null,null,BaseApplication.mCurrentUser.county, schoolCode + "", schoolName);
				}
			}
		}else if(object instanceof UploadAvatarEvent){
			UploadAvatarEvent event  = (UploadAvatarEvent)object;
			fileUpload(event.filePath);
		}else if(object instanceof UpdateInfoEvent){
			setView();
		}
		
	}
	/**
	 * 文件上传
	 * @param filePath
	 */
	public void fileUpload(final String filePath){
		ResponseHandlerInterface response = new BaseJsonHandler<FileUploadResponse>() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, FileUploadResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
//					mAvatarImg.setImageBitmap(BitmapFactory.decodeFile(filePath));
					updateUserInfo("qnTxImgId",response.data.qnPicId, CONSTANT.UPDATE_TYPE_INT);
				} else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			public void onFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
				super.onFailure(statusCode, headers, responseBytes, throwable);
			}
			@Override
			protected FileUploadResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,FileUploadResponse.class);
			}
		};
		HttpManager.getInstance().fileUpload(filePath, response);
	}
	
	@Click({R.id.user_info_nick_layout,R.id.user_info_school_layout,R.id.user_info_level_layout,R.id.user_info_birthday_layout,R.id.user_info_name_layout,R.id.user_info_sex_layout,R.id.user_info_email_layout,R.id.user_info_back,R.id.user_info_avatar_layout,R.id.user_info_nick_txt,R.id.user_info_level_layout,R.id.user_info_account_layout,R.id.user_info_district_layout})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.user_info_back:
			backAction(mFragmentId);
			break;
		case R.id.user_info_avatar_layout:
			showSelectAvatarDialog();
			break;
		case R.id.user_info_name_layout:
			String title = BaseApplication.getResString(R.string.user_info_name);
			String value = TextUtils.isEmpty(mNameValue.getText())?"":mNameValue.getText().toString();
			UpdateUserFragment.add("userName",value, title,mFragmentId);
			break;
		case R.id.user_info_email_layout:
			if(TextUtils.isEmpty(BaseApplication.mCurrentUser.email)) {
				showNotifyDialog(CONSTANT.DIALOG_DOUBLE_CHOICE, 
						getStr(R.string.user_info_notify_email_has), 
						getStr(R.string.common_confirm),
						getStr(R.string.common_cancel), 
						new UserInfoCallBack() {
							
							@Override
							public void userOP() {
								String titleEmail = BaseApplication.getResString(R.string.user_info_email);
								String valueEmail = TextUtils.isEmpty(mEmailValue.getText())?"":mEmailValue.getText().toString();
								UpdateUserFragment.add("email",valueEmail,titleEmail,mFragmentId);
							}
						});
			}else {
				showNotifyDialog(
						CONSTANT.DIALOG_ONE_CHOICE, 
						getStr(R.string.user_info_notify_email_no), 
						getStr(R.string.common_confirm), 
						getStr(R.string.common_cancel), 
						null);
			}
			break;
		case R.id.user_info_sex_layout:
			showSexDialog(BaseApplication.mCurrentUser.sex.key == CONSTANT.SEX_MALE);
			break;
		case R.id.user_info_birthday_layout:
			showDatePicker();
			break;
		case R.id.user_info_level_layout:
			showLevleDialog();
			break;
		case R.id.user_info_account_layout:
			if (TextUtils.isEmpty(BaseApplication.mCurrentUser.mobile)) {
				showNotifyDialog(
						CONSTANT.DIALOG_DOUBLE_CHOICE,
						getStr(R.string.user_info_notify_mobile_has), 
						getStr(R.string.common_confirm), 
						getStr(R.string.common_cancel), 
						new UserInfoCallBack() {
							@Override
							public void userOP() {
								BindMobileFragment.add(mFragmentId);
							}
						});
			}else {
				showNotifyDialog(
						CONSTANT.DIALOG_ONE_CHOICE,
						getStr(R.string.user_info_notify_mobile_no), 
						getStr(R.string.common_confirm), 
						getStr(R.string.common_cancel), 
						null);
			}
			break;
		case R.id.user_info_district_layout:
			Dictionary province = null;
			Dictionary city = null;
			if (BaseApplication.mCurrentUser.province != null) {
				province = new Dictionary(BaseApplication.mCurrentUser.province.key,BaseApplication.mCurrentUser.province.text,Dictionary.TYPE_PROVINCE);
			}
			if (BaseApplication.mCurrentUser.city != null) {
				city = new Dictionary(BaseApplication.mCurrentUser.city.key,BaseApplication.mCurrentUser.city.text,Dictionary.TYPE_CITY);
			}
			SelectAddressFragment.add(mFragmentId,province,city);
			break;
		case R.id.user_info_school_layout:
			if (BaseApplication.mCurrentUser.level == null || BaseApplication.mCurrentUser.level.key < 0) {
				BaseApplication.showToast(R.string.level_select_required);
				return;
			}else if (BaseApplication.mCurrentUser.city == null && BaseApplication.mCurrentUser.city.key <=0) {
				BaseApplication.showToast(R.string.address_select_required);
				return;
			}else{
				Dictionary county = null;
				Dictionary parentCity = null;
				Dictionary school = null;
				Dictionary level = null;
				if (BaseApplication.mCurrentUser.city != null&& BaseApplication.mCurrentUser.city.key > 0) {
					parentCity = new Dictionary(BaseApplication.mCurrentUser.city.key,BaseApplication.mCurrentUser.city.text,Dictionary.TYPE_CITY);
				}
				if (BaseApplication.mCurrentUser.county != null && BaseApplication.mCurrentUser.county.key > 0) {
					county = new Dictionary(BaseApplication.mCurrentUser.county.key,BaseApplication.mCurrentUser.county.text,Dictionary.TYPE_COUNTY);
				}
				if (BaseApplication.mCurrentUser.school != null && BaseApplication.mCurrentUser.school.key > 0) {
					school = new Dictionary(BaseApplication.mCurrentUser.school.key,BaseApplication.mCurrentUser.school.text,Dictionary.TYPE_SCHOOL);
				}
				if (BaseApplication.mCurrentUser.level != null && BaseApplication.mCurrentUser.level.key > 0) {
					level = new Dictionary(BaseApplication.mCurrentUser.level.key,BaseApplication.mCurrentUser.level.text,Dictionary.TYPE_LEVEL);
				}
				if(isNull(parentCity)) {
					BaseApplication.showToast(getStr(R.string.address_select_required));
				}else {
					SelectSchoolFragment.add(mFragmentId, parentCity, county, school, level);
				}
			}
			
			break;
		case R.id.user_info_nick_layout:
			String titleNick = BaseApplication.getResString(R.string.user_info_nick);
			String valueNick = TextUtils.isEmpty(mNickValue.getText())?"":mNickValue.getText().toString();
			UpdateUserFragment.add("nick",valueNick, titleNick,mFragmentId);
			break;
		default:
			break;
		}
	}
	
	private boolean isNull(Dictionary city) {
		return city == null || "".equals(city) ? true : false;
	}
	
	private String getStr(int stringId) {
		return getActivity().getResources().getString(stringId);
	}
	
	Dialog mLevelDialog;
	@SuppressLint("InflateParams")
	@SuppressWarnings("unchecked")
	public void showLevleDialog(){
		RelativeLayout attrLayout = (RelativeLayout) mInflater.inflate(R.layout.level_dialog, null);
		if(mLevelDialog != null && mLevelDialog.isShowing()){
			return;
		}
		mLevelDialog = new Dialog(BaseApplication.mCurrentActivity, R.style.fullscreendialog);
		mLevelDialog.setCanceledOnTouchOutside(true);
		mLevelDialog.setContentView(attrLayout);
		RelativeLayout levelLayout = (RelativeLayout) attrLayout.findViewById(R.id.level_dialog_layout);
		levelList = DictionaryManager.getInstance().queryByField("type",Dictionary.TYPE_LEVEL);
		fillAttrLayout(levelLayout, levelList);
		Window window = mLevelDialog.getWindow();
		window.setBackgroundDrawable(new ColorDrawable(0x00000000));
		window.setLayout(BaseApplication.mScreenWidth-CommonUtil.dip2px(40),WindowManager.LayoutParams.WRAP_CONTENT);
		window.setGravity(Gravity.CENTER);
		window.setWindowAnimations(R.style.dialog_from_bottom);
		window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
				| WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		mLevelDialog.show();
	}
	
	Dialog mSelectAvatarDialog;
	@SuppressLint("InflateParams")
	public void showSelectAvatarDialog(){
		LinearLayout dialogView = (LinearLayout) mInflater.inflate(R.layout.camera_select_dialog, null);
		if(mSelectAvatarDialog != null && mSelectAvatarDialog.isShowing()){
			return;
		}
		mSelectAvatarDialog = buildAlertDialog(dialogView,CommonUtil.dip2px(40));
		TextView camera = (TextView) dialogView.findViewById(R.id.camera_select_dialog_camera);
		TextView album = (TextView) dialogView.findViewById(R.id.camera_select_dialog_album);
		TextView cancle = (TextView) dialogView.findViewById(R.id.camera_select_dialog_cancel);
		camera.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mSelectAvatarDialog.dismiss();
				CameraActivity.show(true, true);
			}
		});
		album.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CameraActivity.show(true,false);
				mSelectAvatarDialog.dismiss();
			}
		});
		cancle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mSelectAvatarDialog.dismiss();
			}
		});
		mSelectAvatarDialog.show();
	}
	
	Dialog mSexDialog;
	@SuppressLint("InflateParams")
	public void showSexDialog(boolean isMale){
		RelativeLayout dialogView = (RelativeLayout) mInflater.inflate(R.layout.sex_select_dialog, null);
		mSexDialog = buildAlertDialog(dialogView,CommonUtil.dip2px(40));
		TextView male = (TextView) dialogView.findViewById(R.id.sex_select_dialog_male);
		TextView female = (TextView) dialogView.findViewById(R.id.sex_select_dialog_femail);
		if (isMale) {
			male.setSelected(true);
			female.setSelected(false);
			male.setTextColor(BaseApplication.getResColor(R.color.theme_red));
			female.setTextColor(BaseApplication.getResColor(R.color.text_color_gray));
		}else{
			male.setSelected(false);
			female.setSelected(true);
			male.setTextColor(BaseApplication.getResColor(R.color.text_color_gray));
			female.setTextColor(BaseApplication.getResColor(R.color.theme_red));
		}
		male.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mSexDialog.dismiss();
				updateUserInfo("sex",CONSTANT.SEX_MALE+"",CONSTANT.UPDATE_TYPE_DIC);
			}
		});
		female.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mSexDialog.dismiss();
				updateUserInfo("sex",CONSTANT.SEX_FEMALE+"",CONSTANT.UPDATE_TYPE_DIC);
			}
		});
		mSexDialog.show();
	}
	Dialog mTimePicker;
	@SuppressLint("InflateParams")
	public void showDatePicker(){
		RelativeLayout pickerLayout = (RelativeLayout) mInflater.inflate(R.layout.date_select_dialog, null);
		if(mTimePicker != null && mTimePicker.isShowing()){
			return;
		}
		mTimePicker = new Dialog(BaseApplication.mCurrentActivity, R.style.fullscreendialog);
		mTimePicker.setCanceledOnTouchOutside(true);
		mTimePicker.setContentView(pickerLayout);
		final BirthdaySelectView wheelMain = new BirthdaySelectView(pickerLayout);
//		wheelMain.setDate(CommonUtil.buildDateArray(6), CommonUtil.buildTimeArray());
		wheelMain.initDate();
		TextView commitTxt = (TextView) pickerLayout.findViewById(R.id.date_select_dialog_confirm);
		TextView cancelTxt = (TextView) pickerLayout.findViewById(R.id.date_select_dialog_cancel);
		commitTxt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LogUtil.d(wheelMain.getTime());
				updateUserInfo("birthday", wheelMain.getTime(), CONSTANT.UPDATE_TYPE_STRING);
				mTimePicker.dismiss();
			}
		});
		cancelTxt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LogUtil.d(wheelMain.getTime());
				mTimePicker.dismiss();
			}
		});
		Window window = mTimePicker.getWindow();
		window.setBackgroundDrawable(new ColorDrawable(0x00000000));
		window.setLayout(BaseApplication.mScreenWidth-CommonUtil.dip2px(40),WindowManager.LayoutParams.WRAP_CONTENT);
		window.setGravity(Gravity.CENTER);
		window.setWindowAnimations(R.style.look_share_show);
		window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
				| WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		mTimePicker.show();
  }
	public void setView(){
		mNickValue.setText(BaseApplication.mCurrentUser.nick);
		if (TextUtils.isEmpty(BaseApplication.mCurrentUser.mobile)) {
			mAccountValue.setText(R.string.user_info_account_bind);
		}else{
			mAccountValue.setText(BaseApplication.mCurrentUser.mobile);
		}
		StringBuffer address = new StringBuffer();
		if (BaseApplication.mCurrentUser.province != null) {
			address.append(BaseApplication.mCurrentUser.province.text).append(" ");
		}
		if (BaseApplication.mCurrentUser.city != null) {
			address.append(BaseApplication.mCurrentUser.city.text).append(" ");
		}
		if (BaseApplication.mCurrentUser.county != null) {
			address.append(BaseApplication.mCurrentUser.county.text).append(" ");
		}
		mDistrictValue.setText(address.toString());
		if (BaseApplication.mCurrentUser.sex != null) {
			mSexValue.setText(BaseApplication.mCurrentUser.sex.text);
		}
		if (BaseApplication.mCurrentUser.level != null) {
			mLevelValue.setText(BaseApplication.mCurrentUser.level.text);
		}
		if (BaseApplication.mCurrentUser.school != null && BaseApplication.mCurrentUser.school.key != -1) {
			mSchoolValue.setText(BaseApplication.mCurrentUser.school.text);
		}else{
			mSchoolValue.setText(R.string.user_info_school_value);
		}
		mAccountValue.setText(BaseApplication.mCurrentUser.mobile);
		mNickValue.setText(BaseApplication.mCurrentUser.nick);
		mNameValue.setText(BaseApplication.mCurrentUser.userName);
		mEmailValue.setText(BaseApplication.mCurrentUser.email);
		mBirthdayValue.setText(BaseApplication.mCurrentUser.birthday);
		mNickValue.setText(BaseApplication.mCurrentUser.nick);
		HttpManager.getInstance().loadEditAvatar(mAvatarImg, BaseApplication.mCurrentUser.imgIdxName,R.drawable.profile_avatar_default);
	}
	
	public void updateUserInfo(String key,String value,int type){
		ResponseHandlerInterface response = new BaseJsonHandler<BaseResponse>() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, BaseResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (response != null && response.getErr() == CONSTANT.NO_ERROR_STATUS) {
					getUserInfo();
				} else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			protected BaseResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,BaseResponse.class);
			}
		};
		HttpManager.getInstance().updateUserInfo(BaseApplication.mUserNo, key, value,type,response);
	}
	
	public void updateSchool(DicMap province, DicMap city,DicMap county,String schoolCode,String schoolName){
		ResponseHandlerInterface response = new BaseJsonHandler<BaseResponse>() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, BaseResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (response != null && response.getErr() == CONSTANT.NO_ERROR_STATUS) {
					getUserInfo();
				} else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			protected BaseResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,BaseResponse.class);
			}
		};
		HttpManager.getInstance().updateAddressSchool(BaseApplication.mUserNo, province,city,county, schoolCode, schoolName, response);
	}
	
	List<Dictionary> levelList = new ArrayList<Dictionary>();
	int mAttrItemMargin = CommonUtil.dip2px(15);
	public void fillAttrLayout(RelativeLayout layout,List<Dictionary> list){
		int previousViewId = 0;
		int firstViewId = 0;
		int attrWidth = mAttrItemMargin;
		int size = list.size();
		for (int i = 0; i < size; i++) {
			final Dictionary dictionary = list.get(i);
			BorderTextView textView = buildAttrTextView(dictionary,i);
			int viewWidth = textView.getViewWidth();
			textView.setBackgroundResource(R.drawable.select_bg_selector);
			if (BaseApplication.mCurrentUser.level != null && BaseApplication.mCurrentUser.level.key == dictionary.code) {
				textView.setSelected(true);
			}
			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			if (i == 0) {
				attrWidth += viewWidth + mAttrItemMargin;
//				lp.addRule(RelativeLayout.BELOW,firstViewId);
				firstViewId = textView.getId();
				lp.setMargins(mAttrItemMargin, mAttrItemMargin, mAttrItemMargin, mAttrItemMargin);
			}else if (attrWidth + viewWidth + mAttrItemMargin > BaseApplication.mScreenWidth ) {//换行
				lp.addRule(RelativeLayout.BELOW,firstViewId);
				lp.setMargins(mAttrItemMargin, 0, mAttrItemMargin, mAttrItemMargin);
				firstViewId = textView.getId();
				attrWidth = viewWidth + mAttrItemMargin;
			}else{
				attrWidth += viewWidth + mAttrItemMargin;
				lp.addRule(RelativeLayout.RIGHT_OF,previousViewId);
				lp.addRule(RelativeLayout.ALIGN_BOTTOM,i*10);
				lp.setMargins(0, 0, mAttrItemMargin, 0);
			}
			textView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					updateUserInfo("level", dictionary.code+"",CONSTANT.UPDATE_TYPE_DIC);
					mLevelDialog.dismiss();
				}
			});
			layout.addView(textView,lp);
			previousViewId = textView.getId();
		}
	}
	
	/**
	 * 创建属性TextView
	 * @param attr  属性描述对象
	 * @param code 属性id
	 * @param value 属性值
	 * @param position 
	 * @return
	 */
	int mAttrPaddingTop = CommonUtil.dip2px(5);
	int mAttrPaddingLeft= CommonUtil.dip2px(8);
	int mAttrMinWidth = CommonUtil.dip2px(50);
	int mSelectColorIndex = 0;
	public BorderTextView buildAttrTextView(final Dictionary dictionary,final int position){
		BorderTextView textView = new BorderTextView(BaseApplication.mContext,R.color.whiteColor);
		textView.setCustomText(dictionary.name,mAttrPaddingLeft*2);
		textView.setTextColor(Color.GRAY);
		textView.setTag(dictionary);
		LogUtil.d("textView.width ==" + textView.getMeasuredWidth());
		textView.setTextSize(14);
		textView.setPadding(mAttrPaddingLeft, mAttrPaddingTop, mAttrPaddingLeft, mAttrPaddingTop);
		textView.setId((position+1)*10);
		textView.setGravity(Gravity.CENTER);
		textView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				todo
			}
		});
		int minWidth = Math.max(mAttrMinWidth, textView.getViewWidth());
		textView.setWidth(minWidth);
		return textView;
	}
	

	/**
	 * 获取用户资料
	 */
	public void getUserInfo(){
		ResponseHandlerInterface response = new BaseJsonHandler<UserResponse>() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, UserResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (response != null && response.getErr() == CONSTANT.NO_ERROR_STATUS) {
					BaseApplication.mCurrentUser = response.getData();
					BaseApplication.mCurrentUser.allDicStr();
					BaseApplication.mCurrentUser.isLogin = 1;
					UserManager.getInstance().insertOrUpdate(BaseApplication.mCurrentUser);
					setView();
				} else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			protected UserResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return mGson.fromJson(rawJsonData,UserResponse.class);
			}
		};
		HttpManager.getInstance().getUserInfo(BaseApplication.mUserNo, response);
	}
	
	@Override
	public void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}
	
	@Override
	public void clear() {
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}
	
	public interface UserInfoCallBack {
		void userOP();
	}
}
